<?php

defined('BASEPATH') or die('forbidden');

class Notice_Controller Extends Controller {

    function _init() {
        check_level(50, segment(0), segment(1), segment(2, 'index'));
    }

    function index() {
        $this->view->display("general/notice/index");
    }

    function flexigrid() {
        list($colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page) = $this->common->get_flexigrid_params();
        $rows = array();
        $sql = "SELECT COUNT(*) AS num FROM {pre}notice WHERE {$where}";
        $one = $this->db->one($sql);
        $total = $one['num'];
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
        $this->db->query("SELECT {$colsinfo} FROM {pre}notice WHERE {$where} {$orderby} LIMIT {$offset},{$rp}");
        while ($n = $this->db->get_array('default', MYSQL_NUM)) {
            $n[5] = get_datetime($n[5]);
            $n[6] = $n[6] ? get_datetime($n[6]) : translate('GENERAL_NOTICE_SHOW_ALLTIME');
            $n[7] = $n[7] ? get_datetime($n[7]) : translate('FOREVER');
            $rows[] = array('id' => $n[0], 'cell' => array_values($n));
        }
        $result = array(
            'page' => $page,
            'total' => $total,
            'rows' => $rows
        );
        echo json_encode($result);
    }

    function add($id = 0) {
        
        if (is_submit()) {
            $rowdata = $_POST['row'];
            $rowdata['createtime'] = time();
            $rowdata['state'] = 0;
            $rowdata['version'] = addslashes(json_encode(explode("\r\n", $rowdata['version'])));
            $rowdata['starttime'] = $rowdata['starttime'] ? strtotime($rowdata['starttime']) : 0;
            $rowdata['expiretime'] = $rowdata['expiretime'] ? strtotime($rowdata['expiretime']) : 0;
            
            $id = $this->db->insert("{pre}notice", $rowdata);
            $this->db->query("UPDATE {pre}notice SET weigh=id WHERE id='{$id}'");
            insert_log(translate('GENERAL_NOTICE_ADD', "id:{$id}"));
            cp_tips("success", $id);
        }
        $this->view->display("general/notice/add");
    }

    function edit($id = 0) {
        if ($id === 0)
            error(translate('C_INVALID_PARAM_ID', $id));
        $row = $this->db->one("SELECT * FROM {pre}notice WHERE id='{$id}'");
        if (!is_array($row))
            error(translate('C_INVALID_PARAM_ID', $id));
        
        if (is_submit()) {
            $rowdata = $_POST['row'];
            $rowdata['version'] = addslashes(json_encode(explode("\r\n", $rowdata['version'])));
            $rowdata['starttime'] = $rowdata['starttime'] ? strtotime($rowdata['starttime']) : 0;
            $rowdata['expiretime'] = $rowdata['expiretime'] ? strtotime($rowdata['expiretime']) : 0;
            
            $this->db->update("{pre}notice", $rowdata, array('id' => $id));
            insert_log(translate('GENERAL_NOTICE_EDIT', "id:{$id}"), $row, $rowdata);
            cp_tips("success", $id);
        }
        $row['version'] = get_json_array($row['version']);
        $this->view->display("general/notice/edit", array('row' => $row));
    }

    function del($id = 0) {
        $c = 0;
        $r = '';
        if ($id === 0)
            json_output($c, translate('C_INVALID_PARAM_ID', $id));
        $row = $this->db->one("SELECT * FROM {pre}notice WHERE id='{$id}'");
        if (!is_array($row))
            json_output($c, translate('C_INVALID_PARAM_ID', $id));
        if ($this->db->delete('{pre}notice', array('id' => $id))) {
            insert_log(translate('GENERAL_NOTICE_DEL', "id:{$id}"));
            $c = 1;
        } else {
            $r = translate('C_TIPS_ERROR_TEXT');
        }
        json_output($c, $r);
    }

    function multi() {
        $type = $this->input->post("type");
        $ids = $this->input->post("ids");
        $c = 0;
        $r = '';
        if (count(explode(',', $ids)) == 0) {
            $r = translate('C_INVALID_PARAMS');
        } else {
            $ids = explode(',', $ids);
            $ids = implode("','", $ids);
            if ($type == 'delete') {
                $this->db->query("DELETE FROM {pre}notice WHERE id IN ('{$ids}')");
                insert_log(translate('GENERAL_NOTICE_MULTI_DEL', "ids:{$ids}"));
                $c = 1;
            } else if ($type == 'accept') {
                $this->db->query("UPDATE {pre}notice SET state='1' WHERE id IN ('{$ids}')");
                insert_log(translate('GENERAL_NOTICE_MULTI_STATUS', "ids:{$ids}][state:1"));
                $c = 1;
            } else if ($type == 'reject') {
                $this->db->query("UPDATE {pre}notice SET state='0' WHERE id IN ('{$ids}')");
                insert_log(translate('GENERAL_NOTICE_MULTI_STATUS', "ids:{$ids}][state:0"));
                $c = 1;
            } else {
                $r = translate('C_INVALID_PARAMS');
            }
        }
        json_output($c, $r);
    }

}